/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.page.menu.inbox;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Message;
import edu.mobbuzz.facade.MessageFacade;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.page.menu.outbox.OutboxPage;
import edu.mobbuzz.util.string.ModeString;

public class MessagePage extends Menu{
    private Form messageForm;
    private Container messsagePanel;
    private Container fromPanel;
    private Label menuTitle;
    private Label fromLbl;
    private Label dateLbl;
    private TextArea titleTxtArea;
    private TextArea descriptionTxtArea;
    private Command backCmd;
    private Command shareCmd;
    private Command selectContactCmd;
    private Message message;
    private Command deleteCmd;
    private int mode;
    private int index;
    private int messageType;
    private MessageFacade messageFacade;
    
    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getMessageType() {
        return messageType;
    }


    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }
    
    public MessagePage(){
        messageForm = new Form();
        messsagePanel = new Container();
        fromPanel = new Container();
        menuTitle = new Label(getMenuTitle());

        fromLbl = new Label();
        dateLbl = new Label();
        titleTxtArea = new TextArea(2,1);
        descriptionTxtArea = new TextArea(5, 10);

        
        shareCmd = new Command("Share") {
            public void actionPerformed(ActionEvent evt) {
                selectContact();
            }
        };

        selectContactCmd = new Command("Continue") {
            public void actionPerformed(ActionEvent evt) {
                selectContact();
            }
        };

        deleteCmd = new Command("Delete") {
            public void actionPerformed(ActionEvent evt) {
                deleteMessage();
            }
        };

        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToInboxOutboxPage();
            }
        };

    }
    

    public void showPage(){
        
        if(getMessage()!=null){
            fromLbl.setText(getMessage().getFrom());
            dateLbl.setText(getMessage().getDate());
            titleTxtArea.setText(getMessage().getTitle());
            descriptionTxtArea.setText(getMessage().getDescription());
            System.out.println(getMessage().getDescription());
        }
        
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);

        fromLbl.getStyle().setMargin(5, 0, 10, 10);
        fromLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        dateLbl.getStyle().setMargin(2, 0, 10, 10);
        
        titleTxtArea.setEditable(false);
        titleTxtArea.setGrowByContent(true);
        titleTxtArea.getStyle().setMargin(10, 0, 10, 10);
        titleTxtArea.getStyle().setBgColor(0x4A657B);
        titleTxtArea.getStyle().setBgSelectionColor(0x4A657B);
        titleTxtArea.getStyle().setFgColor(0xFEF89D);
        titleTxtArea.getStyle().setFgSelectionColor(0xFEF89D);
        titleTxtArea.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        titleTxtArea.getStyle().setBorder(Border.getEmpty());
                
        descriptionTxtArea.setFocusable(true);
        descriptionTxtArea.setGrowByContent(true);
        descriptionTxtArea.getStyle().setMargin(2, 10, 10, 10);

        
        fromPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        fromPanel.addComponent(fromLbl);
        fromPanel.addComponent(dateLbl);
        fromPanel.getStyle().setMargin(5, 0, 10, 10);
        fromPanel.getStyle().setBorder(Border.createLineBorder(1));
        
        messsagePanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        messsagePanel.addComponent(menuTitle);
        //messsagePanel.addComponent(fromLbl);
        //messsagePanel.addComponent(dateLbl);
        messsagePanel.addComponent(fromPanel);
        messsagePanel.addComponent(titleTxtArea);
        messsagePanel.addComponent(descriptionTxtArea);
        messsagePanel.getStyle().setBgColor(0xCCCCCC);

        messageForm.setLayout(new BorderLayout());  
        messageForm.addComponent(BorderLayout.CENTER, messsagePanel);
        messageForm.addCommand(backCmd);

        if(getMode()==ModeString.EDIT){
            messageForm.addCommand(selectContactCmd);
        }else{
            messageForm.addCommand(deleteCmd);
            messageForm.addCommand(shareCmd);            
        }
        
        messageForm.setCommandListener(getActionlistener());
        messageForm.show();
    }


    public void deleteMessage(){
        TextArea body = new TextArea("Delete Message ?", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                getMessageFacade().deleteMessage(getIndex());
                
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);
               
    }
    
    public void selectContact(){
        Message _message = getMessage();
        _message.setDescription(descriptionTxtArea.getText());

        RecepientPage contactPage = new RecepientPage();
        contactPage.setExitCmd(getExitCmd());
        contactPage.setActionlistener(getActionlistener());
        contactPage.setMessage(_message);
        contactPage.setMode(getMode());
        contactPage.setMessageType(getMessageType());
        contactPage.showPage();
    }
    
    public void backToInboxOutboxPage(){
        if(getMessageType()==0){
            InboxPage inboxPage = new InboxPage();
            inboxPage.setExitCmd(getExitCmd());
            inboxPage.setActionlistener(getActionlistener());
            inboxPage.showPage();
        }else if(getMessageType()==1){
            OutboxPage outboxPage = new OutboxPage();
            outboxPage.setExitCmd(getExitCmd());
            outboxPage.setActionlistener(getActionlistener());
            outboxPage.showPage();
        }
        
    }

    public MessageFacade getMessageFacade() {
        if(messageFacade==null){
            messageFacade = new MessageFacade();
        }
        return messageFacade;
    }
    
    public String getMenuTitle() {
        return "Message";
    }

   
    
   
    
    
}
